package com.ikkong.biz.meta.intercept;

import com.ikkong.biz.model.Teacher;
import com.ikkong.core.aop.AopContext;
import com.ikkong.core.constant.ConstShiro;
import com.ikkong.core.dao.Db;
import com.ikkong.core.exception.BizException;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.meta.PageIntercept;
import com.ikkong.core.toolbox.Conver;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.core.toolbox.support.BladePage;
import com.jfinal.kit.JsonKit;

import java.util.List;
import java.util.Map;

public class BizNoticeIntercept extends PageIntercept {

	public void queryBefore(AopContext ac) {
		if (ShiroKit.lacksRole(ConstShiro.ADMINISTRATOR)) {
			//TODO 
			Teacher t = ShiroKit.getTeacher();
			System.out.println(JsonKit.toJson(t));
			if(null == t){
				throw new BizException(ConstShiro.NO_PERMISSION);
			}
			String condition = " and creator = "+t.getId();
			ac.setCondition(condition);
		}
	}

    @SuppressWarnings("unchecked")
    @Override
    public void queryAfter(AopContext ac) {
        BladePage<Map<String, Object>> page = (BladePage<Map<String, Object>>) ac.getObject();
        List<Map<String, Object>> list = page.getRows();
        for (Map<String, Object> map : list) {
            int all = Db.init().queryInt("select count(1) from tb_notice_result where notice_id = #{id}",
                    Record.create().set("id",map.get("id")));
            int receive = Db.init().queryInt("select count(1) from tb_notice_result where notice_id = #{id} and is_read=1",
                    Record.create().set("id",map.get("id")));
            map.put("send_count",receive + "/" +all);
        }
    }

}
